package licode.unisop.realm.application.executor;

import licode.unisop.client.vo.SopUserBrief;
import licode.unisop.provider.api.SiCacheProvider;
import licode.unisop.provider.info.*;
import licode.unisop.provider.utils.StrUtil;
import licode.unisop.provider.vo.*;
import licode.unisop.realm.client.api.SiUserQueryService;
import licode.unisop.types.CacheConf;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 实现用户查询接口
 *
 * @author licode
 */
@Service
public class SiUserQueryExecutor implements SiUserQueryService {
    @Resource
    private UserQueryUtil userQueryUtil;

    @Resource
    private SiCacheProvider siCacheProvider;

    @Override
    public SiAccountSet findAccount(UserIdInfo idFind) {
        return userQueryUtil.findUserPlugin(idFind.getRealmId()).findAccount(idFind);
    }

    @Override
    public SiIdentitySet findIdentity(UserIdInfo idFind) {
        return userQueryUtil.findUserPlugin(idFind.getRealmId()).findIdentity(idFind);
    }

    @Override
    public String lookupUserId(SiAccount account) {
        return userQueryUtil.findUserPlugin(account.getRealmId()).findUserId(account);
    }

    @Override
    public SiUser lookupUserByAccount(SiAccount account) {
        String userId = lookupUserId(account);
        if (StrUtil.isBlank(userId)) {
            return null;
        } else {
            return lookupUserById(UserIdInfo.builder()
                    .realmId(account.getRealmId())
                    .userId(userId)
                    .build());
        }
    }

    @Override
    public SiUser lookupUserById(UserIdInfo info) {
        String key = CacheConf.USER_PREFIX + info.getRealmId() + info.getUserId();
        SiUser siUser = (SiUser)siCacheProvider.getObject(key);

        if (null == siUser) {
            siUser = userQueryUtil.findUserPlugin(info.getRealmId()).findUserById(info);
            if (null != siUser) {
                siCacheProvider.setObject(key, siUser, 60 * 60 * 8);
            }
        }

        return siUser;
    }

    public SiUserBrief toUserBrief(SiUser siUser) {
        if (null != siUser) {
            SiProps props = siUser.getProps() == null ? SiProps.EMPTY : siUser.getProps();

            return SiUserBrief.builder()
                    .userId(siUser.getId())
                    .name(siUser.getName())
                    .nickName(siUser.getNickName())
                    .mobile(siUser.getPhone())
                    .gender(props.getString("gender", "1"))
                    .avatar(props.getString("avatar", ""))
                    .signature(props.getString("signature", ""))
                    .props(new HashMap<>(props))
                    .build();
        } else {
            return null;
        }
    }

    @Override
    public SiUserBrief lookupUserBriefById(UserIdInfo idInfo) {
       return toUserBrief(lookupUserById(idInfo));
    }

    @Override
    public SiUserBrief lookupUserBriefByAccount(SiAccount idInfo) {
        return toUserBrief(lookupUserByAccount(idInfo));
    }

    @Override
    public SiUserGroup lookupUserGroup(UserGroupLookupIn req) {
        return userQueryUtil.findUserPlugin(req.getRealmId()).lookupUserGroup(req);
    }

    @Override
    public List<SiUser> queryUsers(SiQueryUserIn userIn) {
        return userQueryUtil.findUserPlugin(userIn.getRealmId()).queryUsers(userIn);
    }

    @Override
    public SiPageResult<SiUser> findPage(UserPageInfo req) {
        if (null != req.getState() && req.getState() < 0) {
            req.setState(null);
        }
        return userQueryUtil.findUserPlugin(req.getRealmId()).findPage(req);
    }

    @Override
    public List<SiUserGroup> findRealmGroupsOfPlatform(UserGroupQueryIn req) {
        return userQueryUtil.findUserPlugin(req.getRealmId()).findAssignGroup(req);
    }

    @Override
    public SiPageResult<SiUserGroup> findUserGroupPage(UserGroupQueryPageIn req) {
        return userQueryUtil.findUserPlugin(req.getRealmId()).findUserGroupPage(req);
    }

    @Override
    public List<SiRealmPlatform> findRealPlatformOther(SiRealmPlatformOtherIn req) {
        return userQueryUtil.findUserPlugin(req.getRealmId()).findRealPlatformOther(req);
    }

    @Override
    public SiPageResult<SiRealmPlatform> findRealPlatform(SiRealmAssignPlatPageIn req) {
        return userQueryUtil.findUserPlugin(req.getRealmId()).findRealPlatform(req);
    }
}
